import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from "@/stores/index.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/views/home/HomePage.vue'),
    },
    {
      path: '/post/:id',
      name: 'Post',
      component: () => import('@/views/post/PostPage.vue'),
      props:true
    },
    {
      path: '/test',
      name: 'Test',
      component: () => import('@/views/test/TestPage.vue')
    },
    {
      path: '/user',
      name: 'User',
      component: () => import('@/views/user/UserPage.vue')
    },
    {
      path: '/users',
      name: 'OtherUser',
      component: () => import('@/views/user/OtherUserPage.vue')
    },
    {
      path: '/publish',
      name: 'Publish',
      component: () => import('@/views/publish/PublishLayoutContainer.vue'),
      redirect: '/publish/',
      children: [
        {
          path: '/publish/',
          name: 'PublishHome',
          component: ()=> import('@/views/publish/PublishHome.vue')
        },
        {
          path: '/publish/post',
          name: 'PublishPost',
          component: ()=> import('@/views/publish/PublishPost.vue')
        },
        {
          path: '/publish/manage',
          name: 'PublishManage',
          component: ()=> import('@/views/publish/PublishManage.vue')
        },
        {
          path: '/publish/statistics',
          name: 'PublishStatistics',
          component: ()=> import('@/views/publish/PublishStatistics.vue')
        }
      ]
    },
    {
      path: '/search',
      name: 'Search',
      redirect: '/search/post'
    },
    {
      path: '/search/post',
      name: 'SearchPost',
      component: () => import('@/views/search/PostPage.vue')
    },
    {
      path: '/search/tag',
      name: 'SearchTag',
      component: () => import('@/views/search/TagPage.vue'),
      props: true
    },
    {
      path: '/search/user',
      name: 'SearchUser',
      component: () => import('@/views/search/UserPage.vue')
    },
    {
      path: '/user/post',
      name: 'UserPost',
      component: () => import('@/views/user/PostPage.vue')
    },
    {
      path: '/user/like',
      name: 'UserLike',
      component: () => import('@/views/user/LikePage.vue')
    },
    {
      path: '/user/collect',
      name: 'UserCollect',
      component: () => import('@/views/user/CollectPage.vue')
    },
    {
      path: '/user/footprint',
      name: 'UserFootprint',
      component: () => import('@/views/user/FootprintPage.vue')
    },
    {
      path: '/notice',
      name: 'Notice',
      redirect: '/notice/comment'
    },
    {
      path: '/notice/like',
      name: 'NoticeLike',
      component: () => import('@/views/notice/LikePage.vue')
    },
    {
      path: '/notice/comment',
      name: 'NoticeComment',
      component: () => import('@/views/notice/CommentPage.vue')
    },
    {
      path: '/notice/follow',
      name: 'NoticeFollow',
      component: () => import('@/views/notice/FollowPage.vue')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/user/RegisterPage.vue')
    },
    {
      path: '/message',
      name: 'Message',
      component: () => import('@/views/privatemessage/privatemessagePage.vue')
    },
    {
      path: '/set',
      name: 'Set',
      component: () => import('@/views/update/UpdateUserPage.vue')
    },
    {
      path: '/safe',
      name: 'Safe',
      component: () => import('@/views/update/SafePage.vue')
    }
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const protectedRoutes = [
    'User', 'Publish', 'PublishHome', 'PublishPost',
    'PublishManage', 'PublishStatistics', 'Notice', 'NoticeLike',
    'NoticeComment', 'UserPost', 'UserLike', 'UserCollect', 'UserFootprint'
  ];

  const requiresAuth = to.matched.some(record =>
    protectedRoutes.includes(record.name)
  );

  if (requiresAuth) {
    if (userStore.token && userStore.user.nickname) {
      next();
    } else {
      // 保存目标路径用于登录后跳转
      //userStore.setRedirectPath(to.fullPath);
      // 触发显示登录弹窗
      userStore.showLoginModal = true;
      // 取消当前导航
      next(false);
    }
  } else {
    next();
  }
});

export default router
